package com.bw.controller;

import com.alibaba.fastjson2.JSON;
import com.bw.domain.*;
import com.bw.service.OrderService;
import com.bw.service.WorkService;
import com.bw.utils.R;
import com.bw.utils.esypoi.ExportsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author :Beyond🎸🎸
 * @date : 2025-03-08 10:57:23
 * @description Rock and Roll
 **/
@Controller
@RequestMapping("order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    WorkService workService;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("exportExcel2")
    @ResponseBody
    public R exportExcel2 (HttpServletResponse response,String ids){
        List<Order> list = orderService.listByIds(ids);
        ExportsUtil.exports(response,Order.class,list,"工单列表","工单信息","order_");
        return R.OK();
    }

    @RequestMapping("exportExcel")
    @ResponseBody
    public R exportExcel (HttpServletResponse response){
        List<Order> list = orderService.list(null);
        ExportsUtil.exports(response,Order.class,list,"工单列表","工单信息","order_");
        return R.OK();
    }


    /**
    * @Author: Koma🤘🤘
    * @Description: 添加工单
    * @Param: [order]
    * @return: 
    * @Date: 2025/3/11 14:57
    */
@ResponseBody
@RequestMapping("add")
public R add(Order order){
//    工单默认都未处理
    order.setStatusType("0");
    int i = orderService.add(order);

  return i>0?R.OK():R.ERROR(500,"添加失败");
}

    /**
    * @Author: Koma🤘🤘
    * @Description: 社区下拉框
    * @Param: []
    * @return:
    * @Date: 2025/3/11 14:18
    */
    @RequestMapping("listCommunity")
    @ResponseBody
    public R listCommunity(){
//        先查询redis缓存，如果没有再查询数据库 使用list结构
      List<Community>  communityList =  redisTemplate.opsForList().range("communityList",0,-1);
//      如果缓存没有
        if(communityList==null||communityList.size()==0){
            communityList = orderService.listCommunity();
//            将数据存入缓存
            redisTemplate.opsForList().rightPush("communityList",communityList);
        }
        return R.OK(communityList);
    }

    /**
     * @Author: Koma🤘🤘
     * @Description: 工单类型下拉框
     * @Param: []
     * @return:
     * @Date: 2025/3/11 14:18
     */
    @RequestMapping("listType")
    @ResponseBody
    public R listType(String type){
        List<Dict> dictList = orderService.listType(type);
        return R.OK(dictList);
    }
    /**
    * @Author: Koma🤘🤘
    * @Description: 跳转到报修页面
    * @Param: []
    * @return: 
    * @Date: 2025/3/11 14:00
    */
    @RequestMapping("toAdd")
    public String toAdd(){
        return "order/add";
    }



    @RequestMapping("rate")
    @ResponseBody
    public R rate(Rate rate,HttpSession session){
        User userInfo = (User) session.getAttribute("userInfo");

        rate.setUsername(userInfo.getUsername());

        int i = orderService.rate(rate);

        return i>0?R.OK():R.ERROR(500,"评价失败");
    }

    /**
    * @Author: Koma🤘🤘
    * @Description: 获取订单明细
    * @Param: [orderId]
    * @return: 
    * @Date: 2025/3/10 19:09
    */
    @RequestMapping("toDetail")
    public String toDetail(String orderId,Model model){
//        主表
       Order order = orderService.get(orderId);
       model.addAttribute("order",order);
//       明细
        List<Orderdetail> orderDetailList = orderService.getOrderDetail(orderId);
        model.addAttribute("orderDetailList",orderDetailList);
//获取该订单的评论信息
        List<Rate> rateList = orderService.ListRate(orderId);
        model.addAttribute("rateList",rateList);
        return "order/detail";
    }

    @ResponseBody
    @RequestMapping("fp")
    public R fp(Order order, String select, HttpSession httpSession){
//        获取分配人员id
       User user = (User)httpSession.getAttribute("userInfo");
        order.setUserId(user.getId()+"");
       boolean b = orderService.fp(order,select);
       return b?R.OK():R.ERROR(500,"分配人员失败");
    }


    /**
    * @Author: Koma🤘🤘
    * @Description: 工单页面
    * @Param: []
    * @return: 
    * @Date: 2025/3/8 11:00
    */
    @RequestMapping("tolist")
        public String tolist(Model model){
//        社区信息==多选下拉框查询
       List<Community> communityList = orderService.listCommunity();
       model.addAttribute("community", JSON.toJSONString(communityList));
//       分配人员--多选下拉框查询
        List<Worker> workerList = workService.listWorker();
        model.addAttribute("userList",JSON.toJSON(workerList));
        return "order/list";
    }
    
    /**
    * @Author: Koma🤘🤘
    * @Description: 获取工单列表数据
    * @Param: [order, page, limit]
    * @return: 
    * @Date: 2025/3/8 11:01
    */
    @RequestMapping("list")
    @ResponseBody
    public R list(Order order,Integer page,Integer limit){

        PageHelper.startPage(page,limit);
        List<Order> orderList = orderService.list(order);
        PageInfo<Order> info = new PageInfo<>(orderList);
        return R.OK(info);
    }
}
